# 验证前段提交的有关app的信息是否合法
from constant.CommonConstant import SORT_ORDER_ASC
from enums.AppEnum import AppEnum
from enums.AppReviewStatuesEnum import AppReviewStatuesEnum
from enums.AppScoringStategyEnum import AppScoringStrategyEnum
from marshmallow import Schema, fields, validate


class addApp(Schema):
    appName = fields.Str(
        required=True,
        allow_none=False,
        validate=validate.Length(max=80, error="app的名字不得超过80个字符"),
    )
    appDesc = fields.Str(
        required=True,
        allow_none=False
    )
    appType = fields.Int(
        required=True,
        allow_none=False,
        validate=validate.OneOf(AppEnum.get_values(),
                                error="app的类型非法")
    )
    appIcon = fields.Str(
        allow_none=True,
        require_tld=False,
    )
    scoringStrategy = fields.Int(
        required=True,
        allow_none=False,
        validate=validate.OneOf(AppScoringStrategyEnum.get_values(),
                                error="用户评估类型非法")
    )


class updateApp(Schema):
    id = fields.Int(
        required=True
    )
    appName = fields.Str(
        required=True,
        validate=validate.Length(max=80, error="app的名字不得超过80个字符"),
    )
    appDesc = fields.Str(
        required=True
    )
    appType = fields.Int(
        required=True,
        validate=validate.OneOf(AppEnum.get_values(),
                                error="app的类型非法")
    )
    appIcon = fields.URL(
        allow_none=True,
        require_tld=True,  # 要求顶级域名（如 .com/.org）
        relative=False,  # 禁止相对URL（如 /path/to/resource）
        schemes={'http', 'https', 'ftp'}  # 允许的协议
    )
    scoringStrategy = fields.Int(
        required=True,
        validate=validate.OneOf(AppScoringStrategyEnum.get_values(),
                                error="用户评估类型非法")
    )
    reviewStatus = fields.Int(
        allow_none=False,
        validate=validate.OneOf(
            AppReviewStatuesEnum.get_values(),
            error="审核状态异常")
    )
    reviewMessage = fields.Str()
    reviewerId = fields.Int(
        validate=validate.Length(max=1024)
    )


class queryApp(Schema):
    id = fields.Int(
        required=True,
        validate=validate.Range(min=1)
    )
    pageSize = fields.Int(
        default=10,
        validate=validate.Range(min=1)
    )
    current = fields.Int(
        default=1,
        validate=validate.Range(min=1)
    )
    sortField = fields.Str(
        default='id'
    )
    sortOrder = fields.Str(
        default=SORT_ORDER_ASC
    )

    appName = fields.Str(
        allow_none=True,
        validate=validate.Length(max=80, error="appName过长"),
    )
    appDesc = fields.Str(
        allow_none=True,
    )
    appType = fields.Int(
        allow_none=True,
        validate=validate.OneOf(AppEnum.get_values(),
                                error="app的类型非法"),
    )
    scoringStrategy = fields.Int(
        allow_none=True,
        validate=validate.OneOf(
            AppScoringStrategyEnum.get_values(),
            error="用户评估类型非法",
        ),
    )
    reviewStatus = fields.Int(
        allow_none=True,
        # 默认情况下是没有审核的
        validate=validate.OneOf(
            AppReviewStatuesEnum.get_values(),
            error="审核状态异常"
        )
    )
    reviewMessage = fields.Str(
        validate=validate.Length(max=1024),
        allow_none=True,
    )
    reviewerId = fields.Int(
        allow_none=True,
    )


class reviewApp(Schema):
    appId = fields.Int(
        required=True
    )
    reviewStatus = fields.Int(
        required=True,
        validate=validate.OneOf(
            AppReviewStatuesEnum.get_values(),
            error="审核状态异常")
    )
    reviewMessage = fields.Str(
        load_default="审核通过",
        validate=validate.Length(max=1024)
    )
